package com.drugmanage.service.impl;

import com.drugmanage.Base.BaseService;
import com.drugmanage.dao.DrugDao;
import com.drugmanage.dao.ProducerDao;
import com.drugmanage.entity.Drug;
import com.drugmanage.entity.Producer;
import com.drugmanage.query.DrugQuery;
import com.drugmanage.service.IDrugService;
import com.drugmanage.utils.AssertUtil;
import com.drugmanage.utils.DateConversion;
import com.drugmanage.utils.ValidateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DrugService extends BaseService<Drug> implements IDrugService {

    @Resource
    private DrugDao drugDao;
    @Resource
    private ProducerDao producerDao;

    @Override
    public Drug queryById(Integer id) {
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(id), "DrugId is needed");
        Drug drug = drugDao.get(id);
        drug.setDrugTypeStr(drug.getDrugtype()?"西药":"中药");
        return drug;
    }

    @Override
    public void insertDrug(Drug drug) {
        checkParam(drug);
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drugDao.save(drug)), "保存药品失败");
    }

    private void checkParam(Drug drug) {
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drug.getDrugid()), "药品编号未填写");
        Drug drug1 = drugDao.getByDrugId(drug.getDrugid());
        AssertUtil.isTure(null!=drug1&&drug1.getIsvalid(),"该药品已存在！");
        AssertUtil.isTure(drug.getDrugTypeStr() == null, "药品类型未填写");
        AssertUtil.isTure(null==drug.getDrugname()||"".equals(drug.getDrugname()),"药品名称不能为空");
        AssertUtil.isTure(null==drug.getDrugTypeStr()||"".equals(drug.getDrugTypeStr()),"药品类型不能为空");
        drug.setDrugtype((drug.getDrugTypeStr().equals("中药"))?false:true);
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drug.getDrugnum()), "药品 数量不能为空 ");
        AssertUtil.isTure(drug.getSaleprice() == null, "药品价格未填写");
        AssertUtil.isTure(drug.getBuyprice() == null, "药品进价未填写");
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drug.getProducerid()), "生产厂商Id为填写");
        AssertUtil.isTure(producerDao.get(drug.getProducerid())==null,"生产厂商不存在");
        if(drug.getProduceDateStr()!=null){
            drug.setProduceDate(DateConversion.parseDate(drug.getProduceDateStr(),"yyyy-MM-dd"));
        }
        AssertUtil.isTure(drug.getProduceDate() == null, "生产日期未填写");
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drug.getGuaranteePeriod()), "保质期未填写");
        drug.setCreatedate(new Date());
        drug.setUpdatedate(new Date());
        drug.setIsvalid(true);
    }

    @Override
    public void updateDrug(Drug drug) {
//        AssertUtil.isTure(!drug.getIsvalid(),"药品已失效！");
        drug.setDrugtype((drug.getDrugTypeStr().equals("中药"))?false:true);
        drug.setUpdatedate(new Date());
        if(drug.getProduceDateStr()!=null){
            drug.setProduceDate(DateConversion.parseDate(drug.getProduceDateStr(),"yyyy-MM-dd"));
        }
        drugDao.update(drug);
    }

    @Override
    public void deleteDrug(Integer drugId) {
        AssertUtil.isTure(ValidateUtil.nullOrZeroInteger(drugId), "drugId is needed");
        AssertUtil.isTure(drugDao.get(drugId) == null, "this drug does not extist");
        drugDao.delete(drugId);
    }

    @Override
    public Map<String, Object> queryForPage(DrugQuery drugQuery) throws DataAccessException{
        PageHelper.startPage(drugQuery.getPage(),drugQuery.getRows());
        if(drugQuery.getIsValid()==null){
            drugQuery.setIsValid(2);
        }
        if(drugQuery.getDrugType()==null){
            drugQuery.setDrugType(2);
        }

        List<Drug> drugs =drugDao.queryForPage(drugQuery);
        for (Drug drug : drugs) {
            drug.setDrugTypeStr(drug.getDrugtype()?"西药":"中药");
            if(drug.getProduceDate()!=null){
                drug.setProduceDateStr(DateConversion.getDateFormatter(drug.getProduceDate(),"yyyy-MM-dd"));
            }
            Integer producerId = drug.getProducerid();
            if(!ValidateUtil.nullOrZeroInteger(producerId)){
                Producer producer = producerDao.get(producerId);
                if(producer!=null){
                    drug.setProducer(producer.getProducerName());
                }
            }
            if(drug.getDrugnum()<=0){
                drug.setIsvalid(false);
                drug.setDrugnum(0);
            }
        }
        PageInfo<Drug> pageInfo = new PageInfo<Drug>(drugs);
        Map<String,Object> map = new HashMap();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    @Override
    public void deleteDrugs(Integer[] ids) {
        AssertUtil.isTure(ids==null||ids.length<1,"请选择要删除的药品");
        drugDao.deleteBatch(ids);
    }
}
